package com.wkck.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wkck.VO.FrontendDataVo;
import com.wkck.VO.PageResult;
import com.wkck.domain.Notice;
import com.wkck.domain.NoticeContent;
import com.wkck.dto.CompanyWithJobDto;
import com.wkck.dto.FrontendDataDto;
import com.wkck.dto.GetAllNoticesDto;
import com.wkck.dto.ResumeHobbyDto;
import com.wkck.exception.BaseException;
import com.wkck.mapper.HomeFrontendMapper;
import com.wkck.mapper.NoticeContentMapper;
import com.wkck.mapper.NoticeMapper;
import com.wkck.service.HomeFrontendService;
import com.wkck.service.JobJoinService;
import com.wkck.service.NoticeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @Author: CK
 * @Date: 2024/02/23/0:44
 * @Description:
 */
@Service
@Slf4j
public class HomeFrontendServiceImpl implements HomeFrontendService {

    @Autowired
    private HomeFrontendMapper homeFrontendMapper;

    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private NoticeContentMapper contentMapper;

    /**
     * @Author: CK
     * @Date: 2024/2/24
     * @Description: 首页数据
     */
    @Override
    public FrontendDataVo getFrontendData() {

        List<FrontendDataDto> infos = homeFrontendMapper.getJobInfos();
        log.info("over ===============> 1");

        List<CompanyWithJobDto> withJobsInfo = homeFrontendMapper.getCompanyWithJobsInfo();
        log.info("over ===============> 2");

        LambdaQueryWrapper<Notice> informsWrapper = new LambdaQueryWrapper<>();
        informsWrapper
                .eq(Notice::getNotice_type, 1)
                .eq(Notice::getYesapi_sys_notice_status, 0)
                .orderByDesc(Notice::getCreate_time)
                .last("limit 0,5");
        List<Notice> informs = noticeMapper.selectList(informsWrapper);
        log.info("over ===============> 3");

        LambdaQueryWrapper<Notice> noticesWrapper = new LambdaQueryWrapper<>();
        noticesWrapper
                .eq(Notice::getNotice_type, 2)
                .eq(Notice::getYesapi_sys_notice_status, 0)
                .orderByDesc(Notice::getCreate_time)
                .last("limit 0,5");
        List<Notice> notices = noticeMapper.selectList(noticesWrapper);
        log.info("over ===============> 4");

        return FrontendDataVo.builder()
                .careerList(infos)
                .jobs(withJobsInfo)
                .informs(informs)
                .notices(notices)
                .build();
    }

    @Override
    public NoticeContent getPreviewNoticeContent(Long id) {
        if (id == null || String.valueOf(id).equals("undefined")) {
            throw new BaseException("编号为空 不允许预览");
        }

        LambdaQueryWrapper<NoticeContent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(NoticeContent::getNotice_id, id);

        return contentMapper.selectOne(queryWrapper);
    }

    /***
     * @Author: CK
     * @Date: 2024/3/27
     * @Param: [type]
     * @return: @Description: 得到所有通知
     */
    @Override
    public PageResult getAllNotice(GetAllNoticesDto dto) {
        if (Objects.isNull(dto)) {
            throw new BaseException("获取消息异常 原因: 请求参数为空!");
        }

        if (Strings.isEmpty(dto.getType())) {
            throw new BaseException("类型异常 不允许查询!");
        }

        PageHelper.startPage( dto.getCurrentPage(), dto.getSize());
        Page<Notice> page = noticeMapper.getAllNotice(dto);

        return PageResult.builder()
                .total(page.getTotal())
                .records(page.getResult())
                .build();
    }
}
